<html>
<head>
<title>YUI AutoComplete Tests</title>
<link type="text/css" rel="stylesheet" href="../build/logger/assets/logger.css" />
<link type="text/css" rel="stylesheet" href="../build/yuitest/assets/testlogger.css" />
<link type="text/css" rel="stylesheet" href="../build/autocomplete/assets/autocomplete.css" />

<script type="text/javascript" src="../build/yahoo/yahoo-min.js"></script>
<script type="text/javascript" src="../build/dom/dom-min.js"></script>
<script type="text/javascript" src="../build/event/event-min.js"></script>
<script type="text/javascript" src="../build/logger/logger-min.js"></script>
<script type="text/javascript" src="../build/yuitest/yuitest-beta.js"></script>
<script type="text/javascript" src="../build/autocomplete/autocomplete-min.js"></script>

</head>
<body>
<h1>AutoComplete Tests</h1>
<p><input type="button" value="Run Tests" id="btnRun" disabled="true" /></p>

<script type="text/javascript">

(function() {

    var    Dom=YAHOO.util.Dom,
        Assert=YAHOO.util.Assert,
        ObjectAssert=YAHOO.util.ObjectAssert,
        ArrayAssert=YAHOO.util.ArrayAssert,
        DateAssert=YAHOO.util.DateAssert,
        UserAction=YAHOO.util.UserAction,
        TestCase = YAHOO.tool.TestCase,
        TestLogger = YAHOO.tool.TestLogger,
        TestRunner = YAHOO.tool.TestRunner,
        TestSuite = YAHOO.tool.TestSuite,

        DS_JSArray = YAHOO.widget.DS_JSArray,
        DS_JSFunction = YAHOO.widget.DS_JSFunction,
        DS_XHR = YAHOO.widget.DS_XHR,
        AutoComplete = YAHOO.widget.AutoComplete;
        
    function AutoCompleteTestCase(template) {
        AutoCompleteTestCase.superclass.constructor.call(this, template);
        this.input = null;
        this.container = null;
        this.datasource = null;
    };
    YAHOO.lang.extend(AutoCompleteTestCase, TestCase);

    AutoCompleteTestCase.prototype.setUp = function() {
        this.wrapper = document.createElement("div");
        this.wrapper.id = "testACWrapper";
        document.body.appendChild(this.wrapper);
        
        this.input = document.createElement("input");
        this.input.id = "testACInput";
        this.wrapper.appendChild(this.input);

        this.container = document.createElement("div");
        this.container.id = "testACContainer";
        this.wrapper.appendChild(this.container);
        
        this.datasource = new DS_JSArray(["aaa","abc","axyz"]);
    };

    AutoCompleteTestCase.prototype.tearDown = function() {
       if (this.wrapper != null) {
            YAHOO.util.Event.purgeElement(this.wrapper, true);
            document.body.removeChild(this.wrapper);
        }

        this.datasource = null;
    };
        
    AutoCompleteTestCase.prototype.createInstance = function(oConfig) {
        var ac = new AutoComplete(this.input, this.container, this.datasource, oConfig);
        return ac;
    };
    
    var acTemplate = {
        testConstruction: function() {
            var ac = this.createInstance();
            Assert.isObject(ac, "Failed to create basic instance");
            Assert.isInstanceOf(AutoComplete, ac, "Failed to create basic instance");
            ac = null;
        },

        testDestroy: function() {
            var ac = this.createInstance();
            ac.destroy();
            Assert.isNull(ac.dataSource, "Expected datasource to be null");
            Assert.isNull(ac._oContainer, "Expected container to be null");
            Assert.isNull(ac._oTextbox, "Expected textbox to be null");
            ac = null;
        },

        testSetHeader: function() {
            var ac = this.createInstance();
            ac.setHeader("header");
            var el = YAHOO.util.Dom.getElementsByClassName("yui-ac-hd", "div", this.container)[0];
            Assert.areSame("header", el.innerHTML, "Expected header content to be \"header\"");
            ac = null;
        },

        testSetBody: function() {
            var ac = this.createInstance();
            ac.setBody("body");
            var el = YAHOO.util.Dom.getElementsByClassName("yui-ac-bd", "div", this.container)[0];
            Assert.areSame("body", el.innerHTML, "Expected body content to be \"body\"");
            ac = null;
        },

        testSetFooter: function() {
            var ac = this.createInstance();
            ac.setFooter("footer");
            var el = YAHOO.util.Dom.getElementsByClassName("yui-ac-ft", "div", this.container)[0];
            Assert.areSame("footer", el.innerHTML, "Expected footer content to be \"footer\"");
            ac = null;
        },

        testGetListItems: function() {
            var ac = this.createInstance();
            ac.sendQuery("a");
            var els = ac.getListItems();
            Assert.areSame(10, els.length, "Expected 10 items");
            for(var i=0; i<els.length; i++) {
                Assert.areSame("li", els[i].tagName.toLowerCase(), "Expected an LI element");
            }
            
            ac.maxResultsDisplayed = 5;
            ac.sendQuery("a");
            var els = ac.getListItems();
            Assert.areSame(5, els.length, "Expected 5 items");

            ac = null;
        }
    };
    var acTest = new AutoCompleteTestCase(acTemplate);
    acTest.name = "AutoComplete Test Case";

    function runTests(){

        TestRunner.run();
    }

    YAHOO.util.Event.addListener(window, "load", function() {
        var logger = new TestLogger();
        var acsuite = new TestSuite("AutoComplete Test Suite");
        acsuite.add(acTest);
        TestRunner.add(acsuite);

        YAHOO.util.Event.addListener("btnRun", "click", runTests);    
        YAHOO.util.Dom.get("btnRun").disabled = false;

        if (parent && parent != window) {
            YAHOO.tool.TestManager.load();
        }

    });
})();

</script>
</body>
</html>
